package com.useeinfo.oa.open.admin.maintaining;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.maintaining.biz.ApplicationScenariosBiz;
import com.useeinfo.oa.modules.maintaining.entity.ApplicationScenarios;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/web/admin/applicationScenarios")
public class ApplicationScenariosAction {

    @Autowired
    private ApplicationScenariosBiz applicationScenariosBiz;

    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/admin/applicationScenarios/list");
        return modelAndView;
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                               @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                               @RequestParam(value = "searchCode", required = false) String searchCode,
                               HttpServletRequest request) {
        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("searchCode",searchCode);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId()+"");
        }
        return applicationScenariosBiz.findJSONList(queryParam);
    }


    @RequestMapping("/editPage")
    public ModelAndView editPage(@RequestParam(value = "id", required = false) String id,HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView();
        ApplicationScenarios applicationScenarios = applicationScenariosBiz.findModel(StringConverters.ToLong(id));
        modelAndView.addObject("applicationScenarios",applicationScenarios);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        modelAndView.addObject("enterprise",enterprise);
        modelAndView.setViewName("pages/admin/applicationScenarios/edit");
        return modelAndView;
    }

    /**
     * 执行提交的新增或修改请求
     */
    @RequestMapping("/executeEdit")
    @ResponseBody
    public ResultDto executeEdit(ApplicationScenarios applicationScenarios,
                                 HttpServletRequest request) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(enterprise==null){
            return ResultUtil.error("获取企业失败，请更新页面重试");
        }
        if (applicationScenariosBiz.existName(applicationScenarios,enterprise.getId())) {
            return ResultUtil.error("您输入的名称为空或已存在，请重新输入");
        }
        applicationScenarios.setEnterprise(enterprise);
        applicationScenariosBiz.addOrUpdate(applicationScenarios);
        return ResultUtil.success();
    }

    /**
     * 获取系统模块选择数据
     *
     * @param q
     * @return
     */
    @RequestMapping("/getApplicationScenariosSelect")
    @ResponseBody
    public List<SelectVO> getApplicationScenariosSelect(@RequestParam(value = "q", required = false) String q, HttpServletRequest request) {
        List<SelectVO> selectVOS = new ArrayList<>();
        QueryParam queryParam = new QueryParam(1, 20);
        queryParam.getSqlMap().put("searchCode", q);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if(enterprise!=null){
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId()+"");
        }
        List<ApplicationScenarios> applicationScenariosList = applicationScenariosBiz.findList(queryParam);
        applicationScenariosList.forEach(module -> selectVOS.add(new SelectVO(module.getName(), module.getId() + "")));
        return selectVOS;
    }
}
